package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.ConfigDto;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.pojo.SysConfig;
import com.itheima.ydd.service.SysConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;


    @Override
    public ResponseEntity<R<String>> update(List<SysConfig> sysConfigList) {

        for (SysConfig sysConfig : sysConfigList) {
            this.updateById(sysConfig);
        }

       return ResponseEntity.ok(R.success("更新成功"));
    }

    /**
     * 获取系统配置
     * @return
     */
    @Override
    public ResponseEntity<R<Map<String, Object>>> all() {

        Map<String, Object> map = new HashMap<>();
        List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
                .orderByAsc(SysConfig::getSortNum));

        List<SysConfig> allData = new ArrayList<>();
        Map<String, String> configForm = new HashMap<>();
        Map<String, String> configRules = new HashMap<>();

        //// 处理 处理configRules
        LambdaQueryWrapper<SysConfig> lqw1 = new LambdaQueryWrapper<SysConfig>();
        lqw1.eq(SysConfig::getAttrKey,"site_name");
        SysConfig one = this.getOne(lqw1);
        if (ObjectUtils.isEmpty(one)){
            return ResponseEntity.ok(R.err("获取配置规则失败!"));
        }
        String validator = one.getValidator();
        configRules.put("site_name",validator);

        List<ConfigDto> dtoList = configs.stream().map((config) -> {
            ConfigDto dto = new ConfigDto();
            BeanUtils.copyProperties(config,dto);

            //处理 allData
            doConfigValue(dto,config);

            // 处理 configForm
            configForm.put(config.getAttrKey(),config.getAttrValue());

            return dto;
        }).collect(Collectors.toList());


        map.put("allData", dtoList);
        map.put("configForm", configForm);
        map.put("configRules", configRules);

        //获取list
        List<HashMap<String, Object>> list = super.list(new LambdaQueryWrapper<SysConfig>().select(SysConfig::getTabValue).groupBy(SysConfig::getTabValue)).stream().map(config -> {
            HashMap<String, Object> configMap = new HashMap<>();
            switch (config.getTabValue()) {
                case "base":
                    configMap.put("key", "base");
                    configMap.put("name", "基础配置");
                    break;
                case "system":
                    configMap.put("key", "system");
                    configMap.put("name", "系统设置");
                    break;
                case "custom":
                    configMap.put("key", "custom");
                    configMap.put("name", "其他设置");
                    break;
                default:
                    break;
            }
            configMap.put("children",
                    super.list(new LambdaQueryWrapper<SysConfig>().select(SysConfig::getGroupValue)
                            .eq(SysConfig::getTabValue, config.getTabValue()).groupBy(SysConfig::getGroupValue)).stream().map(conf -> {
                        HashMap<String, Object> confMap = new HashMap<>();
                        confMap.put("name", conf.getGroupValue());

                        List<SysConfig> configList = super.list(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getTabValue, config.getTabValue())
                                .eq(SysConfig::getGroupValue, conf.getGroupValue()));
                        List<ConfigDto> configDtoList = configList.stream().map(item -> {
                            ConfigDto configDto = new ConfigDto();
                            BeanUtils.copyProperties(item, configDto);

                            doConfigValue(configDto, item);
                            return configDto;
                        }).collect(Collectors.toList());
                        confMap.put("data", configDtoList);
                        return confMap;
                    }).collect(Collectors.toList()));
            return configMap;
        }).collect(Collectors.toList());

        map.put("list", list);

        return ResponseEntity.ok(R.success(map));
    }


    private ConfigDto doConfigValue(ConfigDto dto, SysConfig config) {

        String config_value = config.getConfigValue();
        if (config_value != null && !"".equals(config_value)) {
            Integer type = config.getType();
            if (type == 4) {
                String[] values = config_value.split("&");
                List<Map<String, Object>> configValue = new ArrayList<>();
                for (String value : values) {
                    String[] split = value.split("=");
                    Map<String, Object> valueHashMap = new HashMap<>();
                    valueHashMap.put("label", split[0]);
                    valueHashMap.put("value", split[1]);
                    configValue.add(valueHashMap);
                }
                dto.setConfigValueList(configValue);
                return dto;
            }
        }
        return null;
    }



}
